package com.lipeng.controller;

import com.lipeng.bean.Game;
import com.lipeng.bean.Master;
import com.lipeng.service.GameService;
import com.lipeng.service.MasterService;
import com.lipeng.utils.CopyPic;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * Created by dllo on 18/3/8.
 */
@Controller
public class MasterController {
    @Resource
    private MasterService masterService;

    @RequestMapping(value = "masterlist")
    public String masterList() {
        return "masterlist";
    }

    @RequestMapping(value = "mastersearch")
    public String masterSearch() {
        return "mastersearch";
    }

    @RequestMapping(value = "selectmaster")
    @ResponseBody
    public Master selectMasterById(@RequestParam("mid") Integer mid) {
        return masterService.selectMasterById(mid);
    }

    @ResponseBody
    @RequestMapping("allmaster")
    public List<Master> selectAllMaster() {
        return masterService.selectAllMaster();
    }

    @RequestMapping(value = "addmaster")
    public String addMaster(MultipartFile file, HttpServletRequest request, Model model) throws IOException {
        String name = request.getParameter("name");
        String intro = request.getParameter("intro");
        String news = request.getParameter("news");
        int age = Integer.valueOf(request.getParameter("age"));

        String midStr = request.getParameter("mid");

        int mid = 0;
        if (midStr!=null){
            mid = Integer.valueOf(midStr);
        }

        Master master = new Master();
        master.setMname(name);
        master.setMintro(intro);
        master.setMage(age);
        master.setNews(news);
        master.setFile(file);
        //保存数据库的路径
        String sqlPath = null;
        //定义文件保存的本地路径
        String localPath = "/Users/dllo/Desktop/frame/GameApp/src/main/webapp/images/";
        String targetPath = "/Users/dllo/Desktop/frame/GameApp/target/GameApp/images/";
        //定义 文件名
        String filename = null;
        if (master.getFile()!=null) {
            //生成uuid作为文件名称
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            //获得文件类型（可以判断如果不是图片，禁止上传）
            String contentType = master.getFile().getContentType();
            //获得文件后缀名
            String suffixName = contentType.substring(contentType.indexOf("/") + 1);
            //得到 文件名
            filename = uuid + "." + suffixName;
            //文件保存路径
            master.getFile().transferTo(new File(localPath + filename));
            CopyPic.copyFile(localPath+filename,targetPath+filename);
            //把图片的相对路径保存至数据库
            sqlPath = "/images/" + filename;
        }else {
            sqlPath = masterService.selectMasterById(mid).getMpic();
        }

        master.setMpic(sqlPath);
        if (mid > 0 ) {
            request.getSession().setAttribute("name", name);
            request.getSession().setAttribute("intro", intro);
            request.getSession().setAttribute("age", age);
            request.getSession().setAttribute("news", news);
            request.getSession().setAttribute("pic", sqlPath);
            return "redirect:updatemaster?mid=" + mid + "";
        }
        int result = masterService.insertMaster(master);
        model.addAttribute("master", master);
        if (result > 0) {
            request.getSession().setAttribute("success", "success");
        } else {
            request.getSession().setAttribute("fail", "fail");
        }
        return "redirect:masterlist";
    }


    @RequestMapping(value = "updatemaster")
    public String updateMaster(HttpServletRequest request, Model model) throws IOException {
        String name = (String) request.getSession().getAttribute("name");
        String intro = (String) request.getSession().getAttribute("intro");
        int age = (int) request.getSession().getAttribute("age");
        String news = (String) request.getSession().getAttribute("news");
        String pic = (String) request.getSession().getAttribute("pic");
        request.getSession().removeAttribute("name");
        request.getSession().removeAttribute("intro");
        request.getSession().removeAttribute("age");
        request.getSession().removeAttribute("news");
        request.getSession().removeAttribute("pic");

        int mid = Integer.valueOf(request.getParameter("mid"));

        Master master = new Master();
        master.setMid(mid);
        master.setMname(name);
        master.setMintro(intro);
        master.setMage(age);
        master.setNews(news);
        master.setMpic(pic);

        int result = masterService.updateMaster(master);
        model.addAttribute("master", master);
        if (result > 0) {
            request.getSession().setAttribute("success", "success");
        } else {
            request.getSession().setAttribute("fail", "fail");
        }
        return "redirect:masterlist";
    }

    @RequestMapping(value = "delmaster")
    public String deleteMasterById(HttpServletRequest request, @RequestParam("mid") Integer mid) {
        int result = masterService.deleteMasterById(mid);
        if (result > 0) {
            request.getSession().setAttribute("success", "success");
        } else {
            request.getSession().setAttribute("fail", "fail");
        }
        return "redirect:masterlist";
    }

    @ResponseBody
    @RequestMapping(value = "searchmastername")
    public List<Master> searchname(@Param("name")String name){
        List<Master> masterList = masterService.selectMasterByName(name);
        return masterList;
    }
}
